package com.waves.spring.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if ("testBean".equals(beanName)) {
            log.info(">>>①实例化之前执行");
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if ("testBean".equals(beanName)) {
            log.info(">>>③实例化之后执行");
            // return false;
        }
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if ("testBean".equals(beanName)) {
            log.info(">>>④依赖注入之前执行，为开发者提供了一个机会去干预属性值的设置过程");
        }
        return pvs;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("testBean".equals(beanName)) {
            log.info(">>>⑨初始化之前执行");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("testBean".equals(beanName)) {
            log.info(">>>①①初始化之后执行");
        }
        return bean;
    }

    @Override
    public void postProcessBeforeDestruction(Object o, String beanName) throws BeansException {
        if ("testBean".equals(beanName)) {
            log.info(">>>①②销毁执行之前执行");
        }
    }
}
